<template>
  <div class="app-container company-container">
    <!-- 搜索和操作区域 -->
    <div class="filter-container">
      <el-input v-model="searchFrom.keywords" placeholder="企业名称" style="width: 200px;" class="filter-item" @keyup.enter="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table v-loading="listLoading" :data="pageInfo.data" border :fit="true" highlight-current-row style="width: 100%;">
      <el-table-column label="ID" prop="id" align="center" min-width="40" />
      <el-table-column label="Logo" align="center">
        <template slot-scope="{ row }">
          <img :src="row.logo" alt="logo" style="width: 100px;height: 100px;">
        </template>
      </el-table-column>
      <el-table-column label="企业名称" prop="name" align="center" />
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{ row }">
          <el-tag :type="row.status | statusFilter">{{ row.status == 0 ? '未启用': (row.status == 1 ? '已启用' : '已禁用') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="地址" prop="address" align="center" />
      <el-table-column label="联系人" prop="contact_user" align="center" />
      <el-table-column label="电话" prop="contact_phone" align="center" />
      <el-table-column label="到期时间" prop="end_date" align="center" />
      <el-table-column label="创建时间" prop="created_at" align="center" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="pageInfo.total > 0"
      :total="pageInfo.total"
      :page.sync="pageInfo.current_page"
      :limit.sync="pageInfo.per_page"
      @pagination="getList"
    />

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="temp.status" class="filter-item">
            <el-option
              v-for="item in statusOptions"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 分页组件
import company from '@/api/company' // 企业API

export default {
  name: 'Company',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'warning',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status] || 'warning'
    }
  },
  data() {
    return {
      listLoading: false,
      searchFrom: {
        keywords: ''
      },
      pageInfo: {
        data: [],
        current_page: 1,
        total: 0,
        per_page: 20,
        search: undefined
      },
      temp: {
        id: undefined,
        name: '',
        status: 'active'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dialogTitle: '',
      statusOptions: ['active', 'inactive']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      // TODO: 调用API获取数据
      this.listLoading = true
      company.list(this.pageInfo.current_page, this.searchFrom.keywords).then(res => {
        this.pageInfo = res.data
      })

      setTimeout(() => {
        this.listLoading = false
      }, 500)
    },
    handleFilter() {
      this.pageInfo.current_page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        status: 'active'
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogTitle = '新增企业'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogTitle = '编辑企业'
      this.dialogFormVisible = true
    },
    createData() {
      // TODO: 调用新增API
      this.dialogFormVisible = false
      this.$message.success('创建成功')
      this.getList()
    },
    updateData() {
      // TODO: 调用更新API
      this.dialogFormVisible = false
      this.$message.success('更新成功')
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('确认删除该企业？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.company-container {
  padding: 20px;
  .filter-container {
    margin-bottom: 20px;
  }
}
</style>
